var app = getApp()
Page({


  data: {
    checkName: '',
    checkPhone: '',
    checkCode:'',
    countTime:60,

  },

  onLoad: function (options) {

  },

  userPhoneInput:function(e){

    this.setData({
      checkPhone: e.detail.value
    })
  },
  userNameInput: function (e) {

    this.setData({
      checkName: e.detail.value
    })
  },
  userCodeInput: function (e) {
    
    this.setData({
      checkCode: e.detail.value
    })
  },

  
  countTime:function(){
    var that = this
    if (this.data.countTime===60 ){// 解决第一次点击时的延时
      this.setData({
        countTime: that.data.countTime - 1
      })
      that.countTime()
    }else{
      setTimeout(function () {
        if (that.data.countTime === 0) {
          that.setData({
            countTime: 60
          })

        } else {
          that.setData({
            countTime: that.data.countTime - 1
          })
          that.countTime()
        }

      }, 1000);
    }
 

  },
  sendCode:function(){
    if (this.data.checkPhone == '' || this.data.checkPhone.length!=11){
      wx.showModal({
        title: '提示',
        content: '请正确输入手机号',
        showCancel:false
      })
    } else if (this.data.countTime!=60){
      wx.showModal({
        title: '提示',
        content: '请稍后',
        showCancel: false
      })
    }else{
      this.countTime()
      var that = this
      wx.request({
        url: 'https://xiaochengxu.fincoder.com/index.php/Home/GetCode/Index/',
        data: {
          mobile: that.data.checkPhone,

        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {

          if (res.data.status === 1) {

          }
          else {
            wx.showModal({
              title: '提示',
              content: '获取失败',
              showCancel: false
            })
          }
        }, fail: function (e) {

        }

      })
    }

  },
  getUserInfo1: function (e) {
    if (this.data.checkPhone.length == 0 || this.data.checkName.length == 0 || this.data.checkCode.length == 0) {
      wx.showModal({
        title: '提示',
        content: '请填写完整信息',
        showCancel: false
      })

    } else if (this.data.checkPhone.length != 11) {
      wx.showModal({
        title: '提示',
        content: '请正确输入手机号',
        showCancel: false
      })
    } else {
      wx.showLoading({
        title: '请稍候...',
      })

      var that = this
      wx.login({
        success: function (res) {
          var code = res.code;
          wx.getUserInfo({
            success: function (ress) {
              that.addInfo(code, ress)
            }, fail: function (ee) {
              wx.hideLoading()
              wx.showModal({
                title: '授权提示',
                content: '小程序需要您的微信授权才能使用哦~ 错过授权页面的处理方法：删除小程序->重新搜索进入->点击授权按钮'
              })
            }
          })        
        }, fail: function () {
        },
        fail: function () {
        }
      })
     
    }
  },
  onGotUserInfo: function (e) {
    console.log('akakaak')
    app.startFromLogin(this.getUserInfo1())

  },
  getUserInfo: function (e) {
    console.log(e)
    console.log('akakaak222222')
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  addInfo: function (code,info) {
   
      var that = this
      console.log('that.data.inviteCode=' + that.data.inviteCode)
        wx.request({
          url: 'https://xiaochengxu.fincoder.com/index.php/Home/RegisterXcx/index',
          data: {
            uname: that.data.checkName,
            mobile: that.data.checkPhone,
            captcha: that.data.checkCode,
            code: code,
      
            rawData: info.rawData,
            encryptedData: info.encryptedData,
            signature: info.signature,
            iv: info.iv,
          },
          header: {
            'content-type': 'application/json'
          },
          success: function (res) {
            wx.hideLoading()
            console.log(res)
         
            if (res.data.status === 1) {
              wx.switchTab({
                url: '../../../info/info',
              })
              app.globalData.myId = res.data.openId
            }
            else {
              wx.hideLoading()
              wx.showModal({
                title: '提示',
                content: '请求失败' + res.data.msg,
                showCancel: false
              })
            }
          }, fail: function (e) {
            wx.hideLoading()
            wx.showModal({
              title: '提示',
              content: '请求失败',
              showCancel: false
            })
          }

        })
      

  },

})